<section class="general-wrapper company">

<section class="company-names">
<?php renderAdForm('left-1');?>

<?php 
$page_url = 'http://stoneselector.com/companies/';
$filterBy = isset($_GET['f']) ? trim($_GET['f']) : 'a';

$alpha=array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z');
?>
<ul class="alpha-bets">
<?php
foreach($alpha as $a):
	$selected = ($filterBy == $a) ? 'class="selected"' : '';
	echo '<li '.$selected.'><a href="'.$page_url.'?f='.$a.'">'.$a.'</a></li>';
		
endforeach;
?>
</ul>


<?php
global $wpdb;


$posts_ids = $wpdb->get_results( "SELECT ID FROM wp_posts
					  			   where post_title LIKE '$filterBy%'");
									   
$ids_list = array();									   
foreach($posts_ids as $id)
{
	$ids_list[] = $id -> ID;
}

//print_r($ids_list);

$args = array(  'cat'				=> 12,
				'posts_per_page'	=> -1,
				'post__in'			=> $ids_list,
				'orderby'			=> 'title',
				'order'				=> 'ASC'
				);			   
									   
									   
		$posts = query_posts($args);
		$cut = ceil( count($posts)/3); // number of tags at column
						
        $capital = '';
        $i = 0; // iterator
						
		$cutter = $cut;
		
		 $output = '<ul>';
		 
		 foreach ( $posts as $post ) {
                            $i++;
							//print_r($post); exit;
                           if ( $i > $cutter ) {
                                    $output .= '</ul><ul>';
                                    $cutter = $cutter + $cut;
                                }
                                $output .= '<li><a href="'.get_permalink().'">' . $post->post_title . '</a> </li>';
                        }
                        echo $output . '</ul>';
?>

<div class="clearfix"></div><!-- Just to increase height automatically -->
</section>
<div class="clearfix"></div><!-- Just to increase height automatically -->

<?php renderAdForm('left-2');?>
</section>
